<?php 
	require ("../connect/connect.php");
	include '../template/template_main.tpl';
	
	if(isset($_SESSION["profile"]) && !($_SESSION["profile"] == "2"))
		header("Location: ../index.php");
	
	if (isset($_POST['pontuacao']))
	{
		$idCorrector = $_SESSION['id'];
		$idResposta = $_POST['idResposta'];
		$pontuacao = $_POST['pontuacao'];
		$query = "UPDATE `respostas_tbl` SET `corrigido`=1, `pontuacao`=$pontuacao ,`id_corretor`=$idCorrector, `data_correcao` = NOW() WHERE `id` = $idResposta";
		
		$conn->query($query);
		
	}
	
	$query = 'SELECT `r`.`id`, `e`.`equipa`, `r`.`resposta`, `r`.`pontuacao` , `r`.`resposta`, `r`.`data_correcao` FROM `respostas_tbl` `r` JOIN `equipas_tbl` `e` ON `r`.id_equipa = `e`.`id` WHERE `r`.`id_jornada` = (SELECT MAX(id) FROM jornadas_tbl WHERE data_fim > NOW())';
		
	$respostas = $conn->query($query);
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
	"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<link rel="stylesheet" type="text/css" href="../css/template.css" />
<link rel="stylesheet" type="text/css" href="../css/corpo_main.css" />
<link rel="stylesheet" type="text/css" href="../css/form.css" />
<script type="text/javascript" src="../js/jquery-1.7.2.js" ></script>
<script type="text/javascript" src="../js/main.js" ></script>
<script type="text/javascript" src="../js/forum.js" ></script>

<title>.:MateMatrix:.</title>
</head>
<body>
	<?php makeHeader(); ?>
	<?php echo $estruturaInicio;?>
	<h1>- Lista de Epocas -</h1>
	<?php
		echo '<div id="forum_corpo">
			<table id="forum_tabela" border=1>
				<tr id="forum_header">
					<th class="">Equipa</th>
					<th class="">Pontuação</th>
					<th class="">Data Correcção</th>
				</tr>';
		
		if ($respostas->num_rows > 0)
		{
			/** 
			 * $row[0] = `r`.`id`
			 * $row[1] = `e`.`equipa`
			 * $row[2] = `r`.`resposta`
			 * $row[3] = `r`.`pontuacao`
			 * $row[4] = `r`.`resposta`
			 * $row[5] = `r`.`data_correcao`
			 */
			$i = 0;
			
			while ($row = $respostas->fetch_row())
			{
				$class = "";
				if (($i%2) == 1)
					$class = "forum_odd_row";
					
				$accao = htmlspecialchars($_SERVER['PHP_SELF']);
				
				$inputs = "<form action=\"$accao\" method=\"post\">";
				$inputs = $inputs . "<input type=\"hidden\" name=\"idResposta\" value=\"$row[0]\"/>";
				$inputs = $inputs . "<select name=\"pontuacao\"><option value=\"10\">10</option><option value=\"9\">9</option><option value=\"8\">8</option><option value=\"7\">7</option><option value=\"6\">6</option><option value=\"5\">5</option><option value=\"4\">4</option><option value=\"3\">3</option><option value=\"2\">2</option><option value=\"1\">1</option><option value=\"0\">0</option></select>";
				$inputs = $inputs . "<input type=\"submit\" value=\"Corrigir\"/></form>";
				
				printf("<tr id=\"%s\" class=\"$class\">
							<td class=\"\"><a onclick=\"mudarMostrar('%s')\" title=\"Texto\">%s</a></td>
							<td class=\"\">%s</td>
							<td class=\"\">$row[5]</td>
						</tr>",
				iconv("windows-1252","utf-8",$row[0]),
				iconv("windows-1252","utf-8",$row[0]),
				htmlspecialchars(iconv("windows-1252","utf-8",$row[1])),
				iconv("windows-1252","utf-8",$row[3]));
				
				
				printf("<tr id=\"msg_%s\" class=\"esconder\"><td colspan=\"3\"><pre class=\"forum_mensagens\">%s<br />$inputs</pre>", iconv("windows-1252","utf-8",$row[0]), htmlspecialchars(iconv("windows-1252","utf-8", $row[4])));
				$i++;
			}
		}
		else 
			echo '<tr><td colspan="3" id="forum_semMsg">Não existem anuncios registadas</td></tr>';
		echo '</table></div>';
		
		echo $estruturaFim;
	?>
	<?php echo $footer;?>
</body>
</html>